<?php

namespace app\admin\controller;

use app\extend\Random;

class Admin extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '用户管理',
                'description' => '管理网站后台管理员',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('info'),
                ),
            ),
        );
    }

    public function index()
    {
        $request = $this->request;

        //筛选条件
        $where = [];
        $pageMaps = [];

        $id = $request->post('id');
        $username = $request->post('username');
        $status = $request->post('status');

        if ($id) {
            $where['id'] = $id;
        }
        if ($username) {
            $where['username'] = $username;
        }
        if ($status) {
            $where['status'] = $status;
        }

        //URL参数
        $pageMaps['id'] = $id;
        $pageMaps['username'] = $username;
        $pageMaps['status'] = $status;

        $pageAry = [
            'list_rows' => 15,
            'page' => input('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('Admin')->loadList($where, $pageAry);
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        return $this->fetch('index');
    }

    public function info()
    {
        $request = $this->request;
        $method = $request->method();
        $id = input('id');
        $adminModel = model('Admin');
        if ($method == 'POST') {
            if ($id) {
                $status = $adminModel->editAll($request);
            } else {
                $status = $adminModel->addall($request);
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url' => url('admin/index')]);
            } else {
                returnJson(0, '操作失败');
            }
        } else {
            $info = $adminModel->getInfo($id);
            $this->assign('info', $info);

            $this->fetch('info');
        }
    }

    public function password()
    {
        $request = $this->request;
        $method = $request->method();
        $id = input('id');
        $adminModel = model('Admin');
        if ($method == 'POST') {
            $status = false;
            $tokenTime = date('Ymd') . ceil(date('H') / 2);

            $password = input('password');
            $twopassword = input('twopassword');
            $token = input('token');

            if ($password != $twopassword) {
                returnJson(0, '两次密码输入不一致');
            }
            if (strlen($password) < 6 || strlen($password) > 16) {
                returnJson(0, '密码长度太短或太长');
            }
            if ($token != $tokenTime) {
                returnJson(0, '口令错误');
            }

            if ($id) {
                $info = $adminModel->getInfo($id);

                $password = md5(md5($password) . $info['id'] . $info['addtime']);

                $temp['id'] = $id;
                $temp['password'] = $password;
                $status = $adminModel->saved($temp);
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url' => url('admin/index')]);
            } else {
                returnJson(0, '操作失败');
            }
        } else {
            $this->assign('id', $id);

            $this->fetch('password');
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        if (model('Admin')->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
}

